<!doctype html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport"
        content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <script type="text/javascript" src="./xlsx.full.min.js"></script>
    <title>js导入导出excel</title>
</head>

<body>

    <input type="file" id="file">

    <a href="javascript:void(0)" id="export">导出</a>

    <div id="table"></div>
</body>

</html>
<script type="text/javascript" src="./export2excel.js"></script>
<script>
    function createTable(arr) {
        var th = '';
        var td = '';
        var tr = '';
        var body = '';
        arr.forEach((ele, index) => {
            td = '';
            for (const key in ele) {
                if (Object.hasOwnProperty.call(ele, key)) {
                    if (index == 0) {
                        th += '<th>' + key + '</th>';
                    }
                    td += '<td>' + ele[key] + '</td>';
                }
            }

            body += '<tr>' + td + '</tr>'
        });

        var table = '<table border="1"><tr>' + th + '</tr>' + body + '</table>';
        console.log(table);
        return table;
    }

    window.onload = function () {
        var file = document.getElementById("file")
        var exp = document.getElementById("export")
        file.onchange = function () {
            var _this = this;
            // 需要注意要导入的Excel文件最好把数据格式转为文本格式
            imExPortPlugin.import({
                "dom": _this,// 导入文件时的input dom
                "format": [],
                "success": function (res) {

                    document.getElementById('table').innerHTML = createTable(res);

                    console.log("最终结果", res)

                }//导入成功后的回调
            })
        }

        exp.onclick = function () {
            var _this = this;
            imExPortPlugin.export({
                "data": [
                    { "id": 1, "time": "2018-08-01" },
                    { "id": 2, "time": "2018-08-02" },
                    { "id": 3, "time": "2018-08-03" },
                    { "id": 4, "time": "2018-08-04" },
                    { "id": 5, "time": "2018-08-05" }
                ],
                "format": [
                    { "old": "id", "new": "序号" },
                    { "old": "time", "new": "时间" },
                ],
                "dom": _this,
                "filename": "测试文件.xlsx",
                "success": function () {
                    console.log("导出成功")
                }
            })
        }

    }
</script>